package net.xdclass.coupon.config;

import lombok.Data;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息队列配置
 */
@Data
@Configuration
//@ConfigurationProperties(prefix = "mqconfig")
public class RabbitMqConfig {

    /** 消息过期时间 */
    private static final String MESSAGE_TTL = "x-message-ttl";
    /** 死信交换机 */
    private static final String DEAD_EXCHANGE = "x-dead-letter-exchange";
    /** 死信路由key */
    private static final String DEAD_ROUTING_KEY = "x-dead-letter-routing-key";

    /**
     * 交换机
     */
    @Value("${mqconfig.coupon_event_exchange}")
    private String eventExchange;

    /**
     * 第一个队列延迟队列，
     */
    @Value("${mqconfig.coupon_release_delay_queue}")
    private String couponReleaseDelayQueue;

    /**
     * 第一个队列的路由key
     * 进入队列的路由key
     */
    @Value("${mqconfig.coupon_release_delay_routing_key}")
    private String couponReleaseDelayRoutingKey;


    /**
     * 第二个队列，被监听恢复库存的队列
     */
    @Value("${mqconfig.coupon_release_queue}")
    private String couponReleaseQueue;

    /**
     * 第二个队列的路由key
     *
     * 即进入死信队列的路由key
     */
    @Value("${mqconfig.coupon_release_routing_key}")
    private String couponReleaseRoutingKey;

    /**
     * 过期时间
     */
    @Value("${mqconfig.ttl}")
    private Integer ttl;

    /**
     * 消息转换器,json形式存储
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 配置交换机【一个微服务一个交换机】
     */
    @Bean
    public Exchange couponExchange(){
        return new TopicExchange(eventExchange, true, false);
    }

    /**
     * 死信队列【一个普通队列】
     */
    @Bean
    public Queue couponReleaseQueue(){
        return new Queue(couponReleaseQueue, true, false, false);
    }

    /**
     * 延迟队列
     */
    @Bean
    public Queue couponReleaseDelayQueue(){
        Map<String, Object> args = new HashMap<>(3);
        args.put(MESSAGE_TTL, ttl);
        args.put(DEAD_EXCHANGE, eventExchange);
        args.put(DEAD_ROUTING_KEY, couponReleaseRoutingKey);
        return new Queue(couponReleaseDelayQueue, true, false, false, args);
    }

    /**
     * 绑定队列到交换机
     * @return
     */
    @Bean
    public Binding couponQueue(){
        return BindingBuilder
                .bind(couponReleaseQueue())
                .to(couponExchange())
                .with(couponReleaseRoutingKey).noargs();
    }

    /**
     * 绑定延迟队列到交换机
     */
    @Bean
    public Binding couponDelayQueue(){
        return BindingBuilder
                .bind(couponReleaseDelayQueue())
                .to(couponExchange())
                .with(couponReleaseDelayRoutingKey).noargs();
    }
}
